package com.fottas.adware.payment.app;

import com.fottas.adware.core.ApiType;
import com.fottas.adware.core.Domain;
import com.fottas.adware.core.WxConstants;
import com.fottas.adware.payment.app.model.WXPayRequest;

import java.util.HashMap;
import java.util.Map;

public class Client {
    public com.fottas.adware.core.Client _client;
    public Client(com.fottas.adware.core.Client client) {
        this._client = client;
    }

    public String pay(WXPayRequest wxPayRequest, boolean isSandbox, Domain domain) throws Exception {
        //默认配置
        Map<String, String> systemParams = new HashMap<>();
        systemParams.put("appid", _client.getConfig(WxConstants.APPID));
        systemParams.put("mch_id", _client.getConfig(WxConstants.MCHID));
        systemParams.put("notify_url", _client.getConfig(WxConstants.NOTIFYURL));
        //非默认配置
        Map<String, String> bizParams = wxPayRequest.toMap();
        systemParams.putAll(bizParams);
        String url = _client.getReqUrl(ApiType.UNIFIED_ORDER, domain, isSandbox);
        if (_client.isCertMode()) {
            return _client.doPostSsl(url, systemParams, _client.getConfig(WxConstants.CERTPATH), _client.getConfig(WxConstants.CERTPASS));
        }
        return _client.doPost(url, systemParams);
    }
}
